package org.duang.render;

import java.io.IOException;

import org.duang.common.exceptios.ServiceException;
import org.duang.config.InstanceFactory;



/**
 * 重定向页面
 */
public class RedirectRender extends Render {
	
	private static final long serialVersionUID = 1812102713097864255L;
	private String url;
	private boolean withQueryString;
	
	public RedirectRender(String url) {
		this.url = url;
	}
	
	public RedirectRender(String url, boolean withQueryString) {
		this.url = url;
		this.withQueryString = withQueryString;
	}
	
	public void render() {
		String contextPath = InstanceFactory.getServletContext().getContextPath();
		if (contextPath != null && url.indexOf("://") == -1)
			url = contextPath + url;
		
		if (withQueryString) {
			String queryString = request.getQueryString();
			if (queryString != null)
				if (url.indexOf("?") == -1)
					url = url + "?" + queryString;
				else
					url = url + "&" + queryString;
		}
		
		try {
			response.sendRedirect(url);
		} catch (IOException e) {
			throw new ServiceException(e);
		}
	}
}

